//
//  PzNetHttp.m
#import "PzNetHttp.h"
#import "AFNetworking/AFNetworking.h"
#import "Pz-Swift.h"
@interface PzNetHttp()
@property (strong, nonatomic) AFHTTPSessionManager *manager;
@end
@implementation PzNetHttp
static PzNetHttp *defaultManager = nil;
 + (PzNetHttp*)defaultRequest{
     static dispatch_once_t token;
     dispatch_once(&token, ^{
         if(defaultManager == nil){
             defaultManager = [[PzNetHttp alloc] init];
         }
     });
     return defaultManager;
}
//网络类初始化
- (instancetype)init{
  if(self = [super init]){
      self.manager = [AFHTTPSessionManager manager];
      self.manager.requestSerializer = [AFHTTPRequestSerializer serializer];
      self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
      self.manager.requestSerializer.timeoutInterval = 10.0;
  }
  return self;
}
//Get请求
- (void)get:(NSString *)apiUrl param:(NSDictionary * __nullable)paramDic success:(HttpRequestSucBlock)successBlack fail:(HttpRequestFailBlock)failBlack{
    [self.manager GET:apiUrl parameters:paramDic headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(responseObject){
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            if ([jsonDict[@"code"] integerValue] == 200) {
                successBlack(jsonDict);
            }else{
                [SVProgressHUD showErrorWithStatus:jsonDict[@"msg"]];
//                [XHToast showCenterWithText:jsonDict[@"msg"]]
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failBlack(error);
    }];
}
- (void)post:(NSString *)apiUrl param:(NSDictionary * __nullable)paramDic success:(HttpRequestSucBlock)successBlack fail:(HttpRequestFailBlock)failBlack{
    [self.manager POST:apiUrl parameters:paramDic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(responseObject){
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            if([jsonDict[@"code"] intValue] == 200){
                successBlack(jsonDict);
            }else{
                if([apiUrl isEqualToString:@"https://pjac.zhjx.lbbtech.cn/zhengjianzhao/api/detectLiveFaceAccurate"]||[apiUrl isEqualToString:@"https://pjac.zhjx.lbbtech.cn/zhengjianzhao/api/koutu"]||[apiUrl isEqualToString:@"https://pjac.zhjx.lbbtech.cn/zhengjianzhao/api/uploaduserfileinfo"]){
                    successBlack(jsonDict);
                }else{
                    [SVProgressHUD showErrorWithStatus:jsonDict[@"msg"]];
                }
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failBlack(error);
    }];
}
@end
